/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.xmlconvertors;

import cz.fi.muni.diplomka.common.enumeration.BuildingType;
import cz.fi.muni.diplomka.common.enumeration.ResearchType;
import cz.muni.fi.diplomka.servicelayer.dto.Research;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Class used for getting Research info from XML
 *
 * @author Jakub Kutil
 */
public class ResearchConvertor {

    private LocationString locationString = new LocationString();
    /**
     * Gets Research from XML
     *
     * @param researchName name of Research to get info about
     *
     * @return research info
     */
    public Research getResearchFromXML(String researchName) {

        Research research = new Research();

        try {

            File fXmlFile = new File(locationString.getXMLLocation() + "\\researches.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            XPath xpath = XPathFactory.newInstance().newXPath();
            String expression = "/researches/research[@id='" + researchName + "']";
            Node node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);

            Element element = (Element) node;

            research.setResearchType(ResearchType.valueOf(element.getAttribute("id")));

            research.setPriceFood(Integer.parseInt(element.getElementsByTagName("priceFood").item(0).getTextContent()));
            research.setPriceGold(Integer.parseInt(element.getElementsByTagName("priceGold").item(0).getTextContent()));
            research.setPriceStone(Integer.parseInt(element.getElementsByTagName("priceStone").item(0).getTextContent()));
            research.setPriceTime(Integer.parseInt(element.getElementsByTagName("priceTime").item(0).getTextContent()));

            Map<BuildingType, Integer> requires = new HashMap<>();

            for (int j = 0; j < element.getElementsByTagName("requires").getLength(); j++) {
                Element e = (Element) element.getElementsByTagName("requires").item(j);
                requires.put(BuildingType.valueOf(e.getElementsByTagName("reqType").item(0).getTextContent()), Integer.parseInt(e.getElementsByTagName("reqLevel").item(0).getTextContent()));
            }

            research.setRequires(requires);

            Map<String, Integer> effects = new HashMap<>();

            for (int j = 0; j < element.getElementsByTagName("effects").getLength(); j++) {
                Element e = (Element) element.getElementsByTagName("effects").item(j);
                effects.put(e.getElementsByTagName("stat").item(0).getTextContent(), Integer.parseInt(e.getElementsByTagName("value").item(0).getTextContent()));
            }

            research.setEffects(effects);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return research;

    }
}
